package com.vitabuybuy.tv.ui.mine.collect.viewmodel

import android.view.View
import androidx.lifecycle.MutableLiveData
import com.library.common.base.BaseViewModel
import com.library.common.base.MessageModel
import com.library.common.ext.rxHttpRequest
import com.library.common.net.error.msg
import com.library.common.toast.XToast
import com.vitabuybuy.tv.dto.MyCollectListDto
import com.vitabuybuy.tv.network.AuthRepository
import com.vitabuybuy.tv.network.MineRepository
import com.vitabuybuy.tv.network.ShopCenterRepository
import java.util.ArrayList

class CollectionViewModel : BaseViewModel(){

    val collectlist = MutableLiveData<MutableList<MyCollectListDto>>()

    fun getMyCollectList(type: String, pageNumber: Int) {
        rxHttpRequest {
            onRequest = {
                collectlist.value = MineRepository.getMyCollectList(type, pageNumber, 10).await()
            }
            onError = {
                XToast.normal(it.msg)
            }
            isLoadingType = true
            isRefresh = pageNumber == 1
        }
    }

    fun collectOrcancel(id: Int, position: Int, isFav: Boolean) {
        rxHttpRequest {
            onRequest = {
                ShopCenterRepository.collectOrcancel(id, isFav).await()
                defUI.msgEvent.postValue(MessageModel(1, arg1 = position))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest= true
        }
    }

    fun collectOrcancelMore(idList: ArrayList<Int>) {
        rxHttpRequest {
            onRequest = {
                MineRepository.collectOrcancelMore(idList).await()
                defUI.msgEvent.postValue(MessageModel(0))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest= true
        }
    }

    fun addToCart(goodId: Int, buyNum: Int) {
        rxHttpRequest {
            onRequest = {
                ShopCenterRepository.addToCart(goodId, buyNum).await()
                defUI.msgEvent.postValue(MessageModel(2))
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }
}
